package cn.kgc.springmvc02.controller;

import cn.kgc.springmvc02.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/5
 * @Description:
 * 基本类型  int  double  long
 * 包装类型 Integer
 * 字符串 String
 * 对象类型  Student
 * 数组
 *
 * 前后端分离开发  前端模板    json 轻量级的数据传输格式 独立于任何一门语言的  JSON

 * json格式的数据
 *
 */
@Controller
@RequestMapping("param")
public class ParamController {


    //    /param/m1?id=1    key名字需要和形参保持一致 否则报错  @RequestParam 指定获取参数的key
    @RequestMapping("m1")
    public String  method1(@RequestParam("number") int id){
        System.out.println("id = " + id);

        return "user";  // 视图名  转发
    }


    @RequestMapping("m2")
    public String  method2(int id,String name){
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        return "user";
    }


    // 添加了@RequestParam注解的参数 默认情况下必须要传参 因为该注解的required属性有默认值  true  -> 改变默认值 false
    @RequestMapping("m3")
    public String  method3(@RequestParam(value = "age",required = false) Integer age){
        System.out.println("age = " + age);
        return "user";
    }


    // 传递参数key 要保持和对象中的属性名字一致
    @RequestMapping("m4")
    public String  method4(User user){
        System.out.println("user = " + user);
        return "user";
    }

    @RequestMapping("m5")
    public String  method5(Integer[] ids){
        for (Integer id : ids) {
            System.out.println("id = " + id);
        }
        return "user";
    }


    // user  java 对象   json {"age":20,"name":"tom"}  -> user  @RequestBody   json
    @RequestMapping("m6")
    public String  method6(@RequestBody User user){
        System.out.println("user = " + user);
        return "user";
    }
}
